/**
 * @(#)ApkConfigController.java 2017年03月1日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.jsbd.boss.domian.Comment;
import org.jsbd.boss.domian.channel.BaseResultVo;
import org.jsbd.boss.enums.ResponseEnum;
import org.jsbd.boss.service.ICommentService;
import org.jsbd.boss.annotation.SystemControllerLog;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.common.ImportExcel;
import org.jsbd.boss.common.ResponseMapUtils;
import org.jsbd.boss.common.ResponseUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.dubbo.common.utils.StringUtils;

@Controller
@RequestMapping("/comment")
public class CommentController {

	public static final Logger logger = LoggerFactory.getLogger(CommentController.class);

	@Autowired
	private ICommentService commentService;

	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(Model model) {
		return "comment/list";
	}

	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public Object save(Comment comment) {
		if (StringUtils.isBlank(comment.getMark())) {
			return ResponseMapUtils.responseFail("mark不能为空");
		}
		comment.setUsed(0);
		commentService.save(comment);
		return ResponseMapUtils.responseSuccess();
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public Object update(Comment comment) {
		if (comment.getId() == null) {
			return ResponseMapUtils.responseFail("参数缺失，请确认参数再提交");
		}
		Comment obj = commentService.getById(comment.getId());
		obj.setMark(comment.getMark());
		obj.setComment(comment.getComment());
		commentService.update(obj);
		return ResponseMapUtils.responseSuccess();
	}

	@RequestMapping(value = "get")
	@ResponseBody
	@SystemControllerLog(description = "获取评论")
	public void get(String mark, HttpServletResponse response) {
		BaseResultVo resultVo = new BaseResultVo();
		try {
			Comment result = commentService.getByMark(mark);
			if (result != null) {
				result.setUsed(1);
				commentService.update(result);
				resultVo.setData(result.toString());
				resultVo.setMessage(ResponseEnum.SUCC.getDesc());
				resultVo.setStatus(ResponseEnum.SUCC.getStatus());
			} else {
				resultVo.setMessage(ResponseEnum.FAIL.getDesc());
				resultVo.setStatus(ResponseEnum.FAIL.getStatus());
			}
		} catch (Exception e) {
			logger.error("获取评论出错", e);
			resultVo.setMessage(ResponseEnum.ERROR.getDesc());
			resultVo.setStatus(ResponseEnum.ERROR.getStatus());
		}
		ResponseUtils.renderJson(response, resultVo.toString());
	}

	@RequestMapping(value = "getList")
	@ResponseBody
	public List<Comment> getList(Comment comment) throws Exception {
		return commentService.getList(comment);
	}

	@RequestMapping(value = "getListPage")
	@ResponseBody
	public Map<String, Object> getListPage(DataGridModel page, Comment comment) throws Exception {
		String mark = comment.getMark();
		if (!StringUtils.isBlank(mark)) {
			String[] marks = mark.split(" ");
			List<String> keywords = new ArrayList<>();
			for (String keyword : marks) {
				keywords.add(keyword);
			}
			comment.setKeywords(keywords);
		}
		return commentService.getListPage(page, comment);
	}
	
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public void delete(Integer[] ids, HttpServletResponse response) {
		try {
			List<Integer> list = Arrays.asList(ids);
			commentService.delete(list);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			ResponseUtils.responseFailure(response);
		}
	}

	@RequestMapping(value = "importExcel", method = RequestMethod.POST)
	@SystemControllerLog(description = "导入excel")
	public void importExcel(@RequestParam("excelFile") MultipartFile file, HttpServletResponse response) {
		try {
			// 读取Excel数据到List中
			List<Comment> list = ImportExcel.readExcel(file, Comment.class);
			commentService.save(list);

			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			logger.error("导入excel出错", e);
			ResponseUtils.responseFailure(response);
		}
	}

}
